#
# Global build configuration variables
#

#
# Read user-provided tools configuration
#
-include $(call select_from_repositories,etc/tools.conf)
-include $(BUILD_BASE_DIR)/etc/tools.conf

#
# Set undefined CUSTOM_ tools to their default values
#
CUSTOM_CC      ?= $(CROSS_DEV_PREFIX)gcc
CUSTOM_CXX     ?= $(CROSS_DEV_PREFIX)g++
CUSTOM_CXX_LIB ?= $(CUSTOM_CXX)
CUSTOM_LD      ?= $(CROSS_DEV_PREFIX)ld
CUSTOM_AS      ?= $(CROSS_DEV_PREFIX)as
CUSTOM_AR      ?= $(CROSS_DEV_PREFIX)ar
CUSTOM_NM      ?= $(CROSS_DEV_PREFIX)nm
CUSTOM_OBJCOPY ?= $(CROSS_DEV_PREFIX)objcopy

#
# Configure shell program before executing any shell commands.
# On Ubuntu the standard shell is dash, which causes problems.
#
SHELL := $(shell which bash)

#
# Some compilers do not support the compiler arguments
# that we use with 'gcc' and, consequently, spit errors.
# Hence, we have to check if the compiler arguments are
# supported and drop them in the other case. We cache
# the result of the check in the CC_OPT_CHECKCC variable.
# The caching improves the build performance by 5 to
# 10 percent.
#
checkcc = $(shell if $(CUSTOM_CC) $(1) -o /dev/null -xc - <<< 'int main(void){return 0;}' &> /dev/null; then echo "$(1)" ; fi ;)

ifeq ($(CC_OPT_CHECKCC),)
CC_OPT_CHECKCC := $(call checkcc, -static)
CC_OPT_CHECKCC += $(call checkcc, -fno-stack-protector)
export CC_OPT_CHECKCC
endif

#
# GNU tools
#
# Non-Linux operating systems may have to install 'findutils'
# to get the GNU versions of xargs and find.
#
TAC       ?= tac
GNU_FIND  ?= find
GNU_XARGS ?= xargs

#
# Options for automatically generating dependency files
#
# We specify the target for the generated dependency file explicitly via
# the -MT option. Unfortunately, this option is handled differently by
# different gcc versions. Older versions used to always append the object
# file to the target. However, gcc-4.3.2 takes the -MT argument literally.
# So we have to specify both the .o file and the .d file. On older gcc
# versions, this results in the .o file to appear twice in the target
# but that is no problem.
#
CC_OPT_DEP    = -MMD -MP -MT '$*.o $*.d'

#
# Configure magic make variables
#
CC            = $(CUSTOM_CC)
CC_OLEVEL    ?= -O2
CC_WARN      ?= -Wall
CC_OPT       += $(CC_OPT_NOSTDINC) -g $(CC_OLEVEL) $(CC_OPT_DEP) $(CC_WARN) $(CC_OPT_CHECKCC)

#
# Warning: we do not use -ffunction-sections and -Wl,-gc-sections by default
# because this mechanism seems to break the exception support on non-Linux
# versions of core.
#

CXX           = $(CUSTOM_CXX)
CC_CXX_OPT   += $(CC_OPT)
CC_C_OPT     += $(CC_OPT)

LD            = $(CUSTOM_LD)
LD_OPT       +=

AS            = $(CUSTOM_AS)
NM            = $(CUSTOM_NM)
OBJCOPY       = $(CUSTOM_OBJCOPY)
ECHO         ?= echo -e

#
# Control sequences for color terminals
#
# To disable colored output, define these variable empty in your
# build-local 'etc/tools.conf' file.
#
BRIGHT_COL  ?= \033[01;33m
DARK_COL    ?= \033[00;33m
DEFAULT_COL ?= \033[0m

ALL_INC_DIR   = .
ALL_INC_DIR  += $(INC_DIR)
ALL_INC_DIR  += $(foreach DIR,$(REP_INC_DIR), $(foreach REP,$(REPOSITORIES),$(REP)/$(DIR)))
ALL_INC_DIR  += $(shell dirname `$(CUSTOM_CXX_LIB) -print-libgcc-file-name`)/include

INSTALL_DIR ?=

VERBOSE     ?= @
VERBOSE_DIR ?= --no-print-directory

MSG_LINK     = @$(ECHO) "    LINK     "
MSG_COMP     = @$(ECHO) "    COMPILE  "
MSG_MERGE    = @$(ECHO) "    MERGE    "
MSG_CONVERT  = @$(ECHO) "    CONVERT  "
MSG_CLEAN    = @$(ECHO) "  CLEAN "
MSG_ASSEM    = @$(ECHO) "    ASSEMBLE "
MSG_INST     = @$(ECHO) "    INSTALL  "
MSG_PRG      = @$(ECHO) "$(BRIGHT_COL)  Program $(DEFAULT_COL)"
MSG_LIB      = @$(ECHO) "$(DARK_COL)  Library $(DEFAULT_COL)"

